/**
 * Copyright (c) 2002-2008 jNETx.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNETx. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNETx.
 *
 * $Id: $
 *
 */
package com.pas.book;

/**
 * @author <a href="mailto:pshmarev@jnetx.ru">Paul Shmarev</a>
 * @version $Revision: $
 */
public class Utils {
    public static final boolean ERROR = true;
    public static final boolean WARN = ERROR && true;
    public static final boolean INFO = WARN && true;
    public static final boolean DEBUG = INFO && true;
    public static final String CHAPTERS_FILE = "chapters";
    public static final String DATA_FOLDER = "data";
    public static final String PART_FILE_PREFIX = "part";
    public static final String INFO_FILE = "info";
    public static final int MAX_ID_LENGTH = 20;

    public static void debug(String message) {
        if (DEBUG) {
            System.out.print(">>>DEBUG ");
            System.out.println(message);
        }
    }

    public static void error(String message, Throwable throwable) {
        if (ERROR) {
            if (message != null && message.length() > 0) {
                System.err.print(">>>ERROR ");
                System.err.println(message);
            }
            if (throwable != null) {
                throwable.printStackTrace();
            }
        }
    }

    public static void error(Object o) {
        if (o instanceof Throwable) {
            error(null, (Throwable) o);
        } else {
            error((String) o, null);
        }
    }

    public static void warn(String message) {
        if (WARN) {
            System.out.print(">>>WARNING ");
            System.out.println(message);
        }
    }

    public static void info(String message) {
        if (INFO) {
            System.out.print(">>>INFO ");
            System.out.println(message);
        }
    }

    public static String createIdFromName(String aName) {
        int length = Math.min(aName.length(), MAX_ID_LENGTH);
        char[] buf = new char[length];
        for (int i = 0; i < length; i++) {
            char c = Character.toLowerCase(aName.charAt(i));
            if ("\t\r\n ".indexOf(c) != -1) {
                buf[i] = '_';
            } else if ("abcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(c) != -1) {
                buf[i] = c;
            } else {
                buf[i] = 'X';
            }
        }
        return String.valueOf(buf);
    }
}
